package MINA;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * @Grup:MINA
 * @Auther:cola
 * @Data:2021/4/6-2021-04-15:00
 * @version:1.0 //服务器端的消息处理器
 */
public class MinaServerHandler extends IoHandlerAdapter {
    //接收消息
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        super.messageReceived(session, message);
        //String msg=(String)message;
        Message msg = (Message) message;
        System.out.println("收到客户端发来的消息:" + msg);
        //向客户端发送消息对象
        msg.setInfo("吃好吃的,不吃免费的");
        msg.setTo(msg.getFrom());
        msg.setFrom("cola");
        session.write(msg);
    }

    //会话关闭
    @Override
    public void sessionClosed(IoSession session) throws Exception {
        super.sessionClosed(session);
        System.out.println("Client closed");
    }

    //一次会话被打开
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        System.out.println("welcome Client" + session.getRemoteAddress());
    }
}
